/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package supermarketv1;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JFrame;
import supermarketv0_class.*;
import javax.swing.JOptionPane;
/**
 *
 * @author ΠΑΝΑΓΙΩΤΗΣ v1
 */
public class StoreManagement extends javax.swing.JFrame {
    Store store;
    SuperMarketMainView1 mainscreen;
    List stores;
    String b1,b2,b3;
    

    /**
     * Creates new form StoreManagement
     */
    public StoreManagement(SuperMarketMainView1  mainscreen,Store store) {
        this.mainscreen=mainscreen;
        this.store=store;
        initComponents();
       
        //εμφάνηση τιμών σε πίνακα 
        stores= mainscreen.db.readStores();
        if(stores.isEmpty()){//απενεργοποίηση πίνακα και κουμπιών
           jTable1.disable();
           jButton2.setEnabled(false);
           jButton3.setEnabled(false);
           
        }
        Object[][] data=new Object[stores.size()][3];//εδώ θα μπουν οι γραμμές του πίνακα
         int i=0;
        Iterator itr=stores.iterator();
        while (itr.hasNext()){
            store=(Store)itr.next();
            //ΟΙ ΣΤΗΛΕΣ ΤΟΥ ΠΙΝΑΚΑ
            data[i][0]=store.getStoreId();
            data[i][1]=store.getName();
            data[i][2]= store.getAddress();
           
            i++;
            String[] columnNames = {"ID","Όνομα","Διεύθυνση"};//τίτλοι των στηλών του πίνακα
            jTable1.setModel(new javax.swing.table.DefaultTableModel( data,columnNames));//δημιουργία πίνακα
            jTable1.repaint();//επανασχεδιασμός πίνακα
            
        }
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(supermarketv1.SuperMarketv1App.class).getContext().getResourceMap(StoreManagement.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jButton2)
                                        .addGap(0, 0, Short.MAX_VALUE))
                                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(18, 18, 18))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(37, 37, 37)
                                .addComponent(jButton4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 413, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(51, 51, 51))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(33, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2)
                        .addGap(18, 18, 18)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton4)
                        .addGap(78, 78, 78))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here: Δημιουργία Καταστήματος
        b1= jButton1.getText();
        this.dispose();
        mainscreen.addstore= new AddStore(mainscreen,null,b1);
        mainscreen.addstore.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        mainscreen.addstore.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:Τροποποίηση Καταστήματος
        b2=jButton2.getText();
        int selectedstore=jTable1.getSelectedRow();//αριθμός επιλεγμένης γραμμής
        if(selectedstore>=0){
            store= (Store)stores.get(selectedstore);
            mainscreen.addstore=new AddStore(mainscreen,store,b2);
            mainscreen.addstore.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            mainscreen.addstore.setVisible(true);
           dispose();
        }
        else{
        //ΑΝ ΔΕΝ ΕΧΕΙΣ ΕΠΙΛΕΞΕΙ ΓΡΑΜΜΗ
            JOptionPane.showMessageDialog(null, "Επιλέξτε Κατάστημα", "Error", JOptionPane.WARNING_MESSAGE);
}
        
        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:Διαγραφή Καταστήματος
        b3= jButton3.getText();
         int selectedstore=jTable1.getSelectedRow();//αριθμός επιλεγμένης γραμμής
        if(selectedstore>=0){
            store= (Store)stores.get(selectedstore);
            mainscreen.addstore=new AddStore(mainscreen,store,b3);
            mainscreen.addstore.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            mainscreen.addstore.setVisible(true);
           dispose();
        }
        else{
        //ΑΝ ΔΕΝ ΕΧΕΙΣ ΕΠΙΛΕΞΕΙ ΓΡΑΜΜΗ
            JOptionPane.showMessageDialog(null, "Επιλέξτε Κατάστημα", "Error", JOptionPane.WARNING_MESSAGE);
}
        
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here: Κλείσιμο
        dispose();// klinei tin forma
       
       
    }//GEN-LAST:event_jButton4ActionPerformed

    /**
     * @param args the command line arguments
     */
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
